/*
 *    Copyright (c) 2018-2025, cloud All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: cloud
 */

import { httpGet, httpPost, request } from '@/http'
import { services } from '@/config'

// 待办
export function getAppingTasksPagerModel(query) {
  return request({
    url: `${services.adminService}flowable/task/getAppingTasksPagerModel`,
    method: 'post',
    data: query
  })
}

// 已办
export function getApplyedTasksPagerModel(query) {
  return request({
    url: `${services.adminService}/flowable/task/getApplyedTasksPagerModel`,
    method: 'post',
    data: query
  })
}
// 我的发起
export function getMySendTaskPageModel(query) {
  return request({
    url: `${services.adminService}flowable/task/getMySendTaskPageModel`,
    method: 'post',
    data: query
  })
}

// 获取发起流程
export function getAppingTaskCont(query) {
  return request({
    url: `${services.adminService}/flowable/task/getAppingTaskCont`,
    method: 'post',
    data: query
  })
}

// 根据实例ID获取审批记录
export function getCommentInfosByProcessInstanceId(processInstanceId) {
  return request({
    url: `${services.adminService}api/flow/getCommentInfosByProcessInstanceId`,
    method: 'post',
    params: { processInstanceId }
  })
}

export const getFlowLogs = businessKey =>
  httpGet(`${services.adminService}flowable/comment/getCommentsByBusinessKey/${businessKey}`)

// 审批
export function complete(obj) {
  return request({
    url: `${services.adminService}api/flow/complete`,
    method: 'post',
    data: obj
  })
}

// 流程分类
export function getCategories(query) {
  return request({
    url: `${services.adminService}flowable/model/getModelCategories`,
    method: 'post',
    data: query
  })
}

// 流程信息
export function getFlowModelInfoPage(query) {
  return request({
    url: `${services.adminService}model-info/getPage`,
    method: 'post',
    data: query
  })
}

// 终止流程
export function stopProcess(obj) {
  return request({
    url: `${services.adminService}api/flow/stopProcess`,
    method: 'post',
    data: obj
  })
}

// 转办
export function assignTask(obj) {
  return request({
    url: `${services.adminService}api/flow/assignTask`,
    method: 'post',
    data: obj
  })
}

// 委派
export function delegateTask(obj) {
  return request({
    url: `${services.adminService}api/flow/delegateTask`,
    method: 'post',
    data: obj
  })
}

// 获取可回退的节点
export function findReturnTaskList(obj) {
  return request({
    url: `${services.adminService}api/flow/getBackNodesByProcessInstanceId/${obj.processInstanceId}/${obj.taskId}`,
    method: 'get'
  })
}

// 回退
export function taskReturn(obj) {
  return request({
    url: `${services.adminService}api/flow/doBackStep`,
    method: 'post',
    data: obj
  })
}

// 前加签
export function beforeAddSignTask(obj) {
  return request({
    url: `${services.adminService}api/flow/beforeAddSignTask`,
    method: 'post',
    data: obj
  })
}

// 后加签
export function afterAddSignTask(obj) {
  return request({
    url: `${services.adminService}api/flow/afterAddSignTask`,
    method: 'post',
    data: obj
  })
}

// 获限可发送节点信息
export function nextFlowNodeList(obj) {
  return request({
    url: `${services.adminService}api/flow/nextFlowNodeList`,
    method: 'post',
    data: obj
  })
}

export const getNextNodes = businessKey =>
  httpGet(`${services.adminService}flowable/context/getNextNodes/${businessKey}`)

// 发送任务（有多个流向时使用，单个流向使用审批方法。）
export function sendUserTask(obj) {
  return request({
    url: `${services.adminService}api/flow/sendUserTask`,
    method: 'post',
    data: obj
  })
}

// 获取高亮线和节点
export function getHighLightedNodeVoByProcessInstanceId(params) {
  return request({
    url: `${services.adminService}api/flow/getHighLightedNodeVoByProcessInstanceId/${params.procInstId}`,
    method: 'post'
  })
}

export const getHighLightedNode = businessKey =>
  httpGet(`${services.adminService}flowable/line/getHighLightedNode/${businessKey}`)

// 查询节点信息
export function getOneActivityVoByProcessInstanceIdAndActivityId(params) {
  return request({
    url: `${services.adminService}api/flow/getOneActivityVoByProcessInstanceIdAndActivityId/${params.procInstId}/${params.elementId}`,
    method: 'post'
  })
}
export const getNodeInfo = (businessKey, nodeId) =>
  httpGet(`${services.adminService}flowable/node/getNodeInfo/${businessKey}/${nodeId}`)
// 通过modelKey获取模型对象
export function loadBpmnXmlByModelKey(params) {
  return request({
    url: `${services.adminService}api/flow/loadBpmnXmlByModelKey/${params.modelKey}`,
    method: 'post'
  })
}

export const loadFlowByModelKey = modelKey =>
  httpGet(`${services.adminService}flowable/model/loadModelByKey/${modelKey}`)
export const saveFlowByModel = ({ appSn, categoryCode, fileName, id, modelId, modelKey, modelName, modelXml }) =>
  httpPost(`${services.adminService}flowable/model/saveModel`, {
    appSn,
    categoryCode,
    fileName,
    id,
    modelId,
    modelKey,
    modelName,
    modelXml
  })

export const validateFlowModel = ({ appSn, categoryCode, fileName, id, modelId, modelKey, modelName, modelXml }) =>
  httpPost(`${services.adminService}flowable/model/validateModel`, {
    appSn,
    categoryCode,
    fileName,
    id,
    modelId,
    modelKey,
    modelName,
    modelXml
  })

export const publishBpmn = modelId => httpPost(`${services.adminService}flowable/model/publishModel/${modelId}`)

// 撤回
export function revokeProcess(params) {
  return request({
    url: `${services.adminService}api/flow/revokeProcess`,
    method: 'post',
    data: params
  })
}

// 驳回任务
export function taskReject(params) {
  return request({
    url: `${services.adminService}api/flow/taskReject`,
    method: 'post',
    data: params
  })
}

// 签收
export function claimTask(obj) {
  return request({
    url: `${services.adminService}api/flow/claimTask`,
    method: 'post',
    data: obj
  })
}

// 反收签
export function unClaimTask(obj) {
  return request({
    url: `${services.adminService}api/flow/unClaimTask`,
    method: 'post',
    data: obj
  })
}

// 查询待签收任务
export function getTotoSignTaskList(query) {
  return request({
    url: `${services.adminService}api/flow/getTotoSignTaskList`,
    method: 'post',
    data: query
  })
}

// 根据procInstId查询当前用户办理taskVo
export function getTaskVoByProcessInstanceId(procInstId) {
  return request({
    url: `${services.adminService}api/flow/getTaskVoByProcessInstanceId/${procInstId}`,
    method: 'post'
  })
}

export function getTaskVoByBusinessKey(businessKey) {
  return request({
    url: `${services.adminService}api/flow/getTaskVoByBusinessKey/${businessKey}`,
    method: 'post'
  })
}

export const getCurrentNodes = businessKey =>
  httpGet(`${services.adminService}flowable/context/getCurrentNodes/${businessKey}`)
export const getFlowContext = (businessKey, taskId) =>
  httpGet(`${services.adminService}flowable/context/getFlowContext/${businessKey}`, { taskId })
export const getBackNodes = taskId => httpGet(`${services.adminService}flowable/context/getBackNodes/${taskId}`)

// 根据taskId获取任务对象
export function getAppingTasksModel(taskId) {
  return request({
    url: `${services.adminService}api/flow/getAppingTasksModel/${taskId}`,
    method: 'post'
  })
}

// 检查已办任务是否有提取功能
export function checkHistoryTaskDraw(data) {
  return request({
    url: `${services.adminService}api/flow/checkHistoryTaskDraw`,
    method: 'post',
    data
  })
}
// 提取已办行务
export function drawHistoryTask(data) {
  return request({
    url: `${services.adminService}api/flow/drawHistoryTask`,
    method: 'post',
    data
  })
}

export const startProcess = ({ processDefinitionKey, businessKey, formId, variables, title }) =>
  httpPost(`${services.adminService}flowable/runtime/startProcess`, {
    processDefinitionKey,
    businessKey,
    formId,
    variables,
    title
  })
export const completeTask = ({ taskId, businessKey, handlers, variables, advice }) =>
  httpPost(`${services.adminService}flowable/runtime/completeTask`, {
    taskId,
    businessKey,
    handlers,
    variables,
    advice
  })
export const send = ({ taskId, businessKey, handlers, variables, advice, nextNodeId }) =>
  httpPost(`${services.adminService}flowable/runtime/send`, {
    taskId,
    businessKey,
    handlers,
    variables,
    advice,
    nextNodeId
  })
export const reject = ({ taskId, businessKey, variables, advice, targetNodeId }) =>
  httpPost(`${services.adminService}flowable/runtime/reject`, { taskId, businessKey, variables, advice, targetNodeId })
export const withdraw = ({ taskId, businessKey, variables, advice, targetNodeId }) =>
  httpPost(`${services.adminService}flowable/runtime/withdraw`, {
    taskId,
    businessKey,
    variables,
    advice,
    targetNodeId
  })
